package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(@RequestBody Employee employee, HttpSession session){
        String username = employee.getUsername();
        String password = employee.getPassword();
        //对密码进行md5加密，用于与数据库进行了md5加密的密码进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名进行查找
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,username);
        Employee one = employeeService.getOne(queryWrapper);
        //没有人直接返回失败
        if (null == one){
            return R.error("登录失败");
        }
        //有人则验证密码
        if(!one.getPassword().equals(password)){
            return R.error("登录失败");
        }
        //验证状态
        if(one.getStatus()== 0 ){
            return R.error("登录失败");
        }
        //添加session，保存用户状态
        session.setAttribute("employee",one);
        //将session中的用户id放入ThreadLocal中
        BaseContext.setCurrentId(one.getId());

        //通过
        return R.success(employee);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.invalidate();//TODO:不清除直接返回成功，会有什么效果
        return R.success("登出成功");
    }

    @PostMapping
    public R<String> save(@RequestBody Employee employee,HttpSession session){
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());
        employee.setPassword(password);

//        //设置创建时间与更新时间
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        //获取session用户中的id
//        Employee one = (Employee) session.getAttribute("employee");
//        Long id = one.getId();
//
//        //设置创建人与更新人
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        employeeService.save(employee);
        return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Employee>> page(Integer page,Integer pageSize,String name){
        //创建分页构造器
        Page<Employee> pageInfo = new Page<>(page, pageSize);
        //查询条件
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //StringUtils.isNotBlank(name)是Mybatis-plus提供的工具类
        queryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        //排序方法
        queryWrapper.orderByDesc(Employee::getUpdateTime);
        //分页查询
        employeeService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);

    }

    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        return R.success(employee);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee,HttpSession session){
//        employee.setUpdateTime(LocalDateTime.now());
//        employee.setUpdateUser(((Employee)session.getAttribute("employee")).getId());
        employeeService.updateById(employee);
        return R.success("更新成功");
    }

}
